/*
* @Description:合约管理-部署
* @Author: lil
* @Date: 2021/11/24
**/
<template>
  <div v-loading="loading">
    <!-- 表单 -->
    <el-form :model="form" :rules="rules" ref="form" label-width="158px">
      <el-form-item label="选择节点:" prop="checkList1">
        <template v-if="peers.length>0">
          <el-checkbox-group v-model="form.checkList1">
            <el-checkbox :disabled="true" v-for="(data,idx) in peers" :label="data.label" :key="idx"> </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>暂无节点可选</template>
      </el-form-item>
      <el-form-item label="选择通道:" prop="checkList2">
        <template v-if="channels.length>0">
          <el-checkbox-group v-model="form.checkList2">
            <el-checkbox :disabled="true" v-for="(data,idx) in channels" :label="data.label" :key="idx">
            </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>暂无通道可选</template>
      </el-form-item>
      <el-form-item label="已部署该合约的组织:" prop="checkList3">
        <template v-if="orgs.length>0">
          <el-checkbox-group v-model="form.checkList3">
            <el-checkbox :disabled="true" v-for="(data,idx) in orgs" :label="data.label" :key="idx"> </el-checkbox>
          </el-checkbox-group>
        </template>
        <template v-else>暂无已部署过该合约的组织</template>
      </el-form-item>
      <el-form-item label="链码实例化参数:" prop="chainCodeInstantParam">
        <el-input v-model="form.chainCodeInstantParam" placeholder="请输入链码实例化参数"></el-input>
      </el-form-item>
      <el-form-item label-width="0" class="btns">
        <el-button type="primary" :disabled="peers.length==0" @click="deploy()">部署</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import {
    AllCCInstallInfo,
  } from "@/api/blockchain/blockchaindeploy";
  import {
    instantiateResolution
  } from "@/api/blockchain/chaincodeinit";
  export default {
    props: ["versionCode", "chainCodeName"],
    data() {
      return {
        loading: false,
        peers: [],
        channels: [],
        orgs: [],
        form: {
          checkList1: [],
          checkList2: [],
          checkList3: [],
          chainCodeInstantParam: "",
        },
        rules: {
          checkList1: [{
            type: "array",
            required: true,
            message: "请至少选择一个节点！",
            trigger: "change",
          }, ],
          checkList2: [{
            type: "array",
            required: true,
            message: "请至少选择一个通道！",
            trigger: "change",
          }, ],
          checkList3: [{
            type: "array",
            required: true,
            message: "请至少选择一个部署过该合约的组织！",
            trigger: "change",
          }, ],
        },
      };
    },

    mounted() {
      this.getAllPeerChannelOrgs();
    },

    methods: {
      getAllPeerChannelOrgs() {
        var _this = this;
        AllCCInstallInfo(this.chainCodeName)
          .then(function (res) {
            _this.form.checkList1 = res.data.peer;
            _this.form.checkList2 = res.data.channel;
            _this.form.checkList3 = res.data.orgname;
            _this.peers = [];
            for (let i = 0; i < res.data.peer.length; i++) {
              _this.peers.push({
                label: ""
              });
              _this.peers[i].label = res.data.peer[i];
            }

            _this.channels = [];
            for (let i = 0; i < res.data.channel.length; i++) {
              _this.channels.push({
                label: ""
              });
              _this.channels[i].label = res.data.channel[i];
            }

            _this.orgs = [];
            for (let i = 0; i < res.data.orgname.length; i++) {
              _this.orgs.push({
                label: ""
              });
              _this.orgs[i].label = res.data.orgname[i];
            }
          })
          .catch((err) => {
            console.log(err);
          });
      },

      deploy() {
        let peer = "";
        for (let item of this.form.checkList1) {
          if (peer.length == 0) {
            peer = peer + item;
          } else {
            peer = peer + "," + item;
          }
        }
        let peerDeploy = peer.split(",");
        let channelNames = "";
        for (let item of this.form.checkList2) {
          if (channelNames.length == 0) {
            channelNames = channelNames + item;
          } else {
            channelNames = channelNames + "," + item;
          }
        }
        let channelDeploy = channelNames.split(",");
        let orgNames = "";
        for (let item of this.form.checkList3) {
          if (orgNames.length == 0) {
            orgNames = orgNames + "peer" + item;
          } else {
            orgNames = orgNames + "," + "peer" + item;
          }
        }
        let orgDeploy = orgNames.split(",");
        let args = this.form.chainCodeInstantParam.split(",");
        this.$refs.form.validate((valid) => {
          if (valid) {
            var _this = this;
            _this.$emit('setLoading', true);
            instantiateResolution(
                peerDeploy,
                orgDeploy,
                channelDeploy,
                this.chainCodeName,
                this.versionCode,
                args
              )
              .then((res) => {
                _this.$message.success("合约部署成功！");
                _this.$emit('setLoading', false);
                _this.$emit("close");
              })
              .catch((err) => {
                _this.$emit('setLoading', false);
                console.log(err);
              });
          }
        });
      },
    },
  };

</script>
<style lang="scss" scoped>
  .el-form {
    padding: 40px 40px 10px;

    .el-form-item:not(:nth-child(4)) {
      &>>>.el-form-item__label {
        &::before {
          content: "*";
          display: inline-block;
          color: #f12945;
        }
      }
    }

    .btns {
      text-align: center;
      margin-top: 70px;

      .el-button:nth-of-type(1) {
        margin-right: 40px;
      }
    }
  }

</style>
